package com.example.sunmiv1.utils.imgutils;

/**
 * Created by Administrator on 2017/6/2.
 */

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * 圖片壓縮工具類
 * @author touch_ping
 * 2015-1-5 下午1:29:59
 */
public class BitmapCompressor {
    /**
     * 質量壓縮
     * @author ping 2015-1-5 下午1:29:58
     * @param image
     * @param maxkb
     * @return
     */
    public static Bitmap compressBitmap(Bitmap image, int maxkb) {
        //L.showlog(壓縮圖片);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 50, baos);// 質量壓縮方法，這裏100表示不壓縮，把壓縮後的數據存放到baos中
        int options = 100;
//      Log.i(test,原始大小 + baos.toByteArray().length);
        while (baos.toByteArray().length / 1024 > maxkb) { // 循環判斷如果壓縮後圖片是否大於(maxkb)50kb,大於繼續壓縮
//          Log.i(test,壓縮一次!);
            baos.reset();// 重置baos即清空baos
            options -= 10;// 每次都減少10
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 這裏壓縮options%，把壓縮後的數據存放到baos中
        }
//      Log.i(test,壓縮後大小 + baos.toByteArray().length);
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把壓縮後的數據baos存放到ByteArrayInputStream中
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream數據生成圖片
        return bitmap;
    }

    /**
     * http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
     * 官網：獲取壓縮後的圖片
     *
     * @param res
     * @param resId
     * @param reqWidth
     *            所需圖片壓縮尺寸最小寬度
     * @param reqHeight
     *            所需圖片壓縮尺寸最小高度
     * @return
     */
    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

    /**
     * 官網：獲取壓縮後的圖片
     *
     *
     * @param reqWidth
     *            所需圖片壓縮尺寸最小寬度
     * @param reqHeight
     *            所需圖片壓縮尺寸最小高度
     * @return
     */
    public static Bitmap decodeSampledBitmapFromFile(String filepath, int reqWidth, int reqHeight) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filepath, options);

        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filepath, options);
    }

    public static Bitmap decodeSampledBitmapFromBitmap(Bitmap bitmap, int reqWidth, int reqHeight) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, baos);
        byte[] data = baos.toByteArray();

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(data, 0, data.length, options);
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeByteArray(data, 0, data.length, options);
    }

    /**
     * 計算壓縮比例值(改進版 by touch_ping)
     *
     * 原版2>4>8...倍壓縮
     * 當前2>3>4...倍壓縮
     *
     * @param options
     *            解析圖片的配置信息
     * @param reqWidth
     *            所需圖片壓縮尺寸最小寬度O
     * @param reqHeight
     *            所需圖片壓縮尺寸最小高度
     * @return
     */
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

        final int picheight = options.outHeight;
        final int picwidth = options.outWidth;
        Log.i("test", "原尺寸: "+  picwidth + "* "+picheight);

        int targetheight = picheight;
        int targetwidth = picwidth;
        int inSampleSize = 1;

        if (targetheight > reqHeight || targetwidth > reqWidth) {
            while (targetheight  >= reqHeight
                    && targetwidth>= reqWidth) {
                Log.i("test","壓縮: "+inSampleSize +"倍");
                inSampleSize += 1;
                targetheight = picheight/inSampleSize;
                targetwidth = picwidth/inSampleSize;
            }
        }

        Log.i("test","最終壓縮比例:" +inSampleSize + "倍");
        Log.i("test", "新尺寸:"+  targetwidth + "*" +targetheight);
        return inSampleSize;
    }
}
